﻿// NNGTest1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <string>
#include<windows.h>
#include<jsoncpp/json.h>
#include <fstream>
#include "NNGController.h"
using std::string;

string url_test1 = "ipc:///tmp/pipeline.ipc";

class camparam
{
public:
    string camid;
    string camgain;
    string camexp;
};

class detparam
{
public:
    string detid;
    string detimg;
};

class jsonobj
{
public:
    string msgtype;
    string time;
    string objectid;
    string action;
    string fromid;
    string gotoid;
    string params;
    string msgkey1;
    string msgkey2;
    string msgkey3;
};
void writeFile(string instr)
{
    string filename = "aaa.json";
    std::ofstream ofs;
    ofs.open(filename, std::ios::out);
    ofs << instr;
}

/// <summary>
/// 从文件读取Json
/// </summary>
/// <param name="instr"></param>
void readFile(string& instr)
{
    string filename = "aaa.json";
    std::ifstream ifs;
    ifs.open(filename, std::ios::in);
    string content((std::istreambuf_iterator<char>(ifs)),
        (std::istreambuf_iterator<char>()));
    instr = content;
    //ifs >> instr;
}

/// <summary>
/// 通过代码生成Json
/// </summary>
void CreateJsonStr()
{
    // 将Json对象，序列化为一个字符串
    Json::FastWriter writer;
    Json::Value value;
    value["detid"] = "123456";
    value["detimg"] = "kkkk";
    string jsonstr = writer.write(value);
    writeFile(jsonstr);
}

/// <summary>
/// 字符串序列化成对象
/// </summary>
void CreateJsonObj()
{
    //将Json字符串转换成Json对象
    string jsonstr = "";
    readFile(jsonstr);
    std::cout <<"读取到的字符串:"<< jsonstr;

    Json::Reader reader;
    Json::Value value;
    if (!reader.parse(jsonstr, value, false))
        throw string("bad json format!\n");
    std::cout << "detid:" << value["detid"];
    std::cout << "detimg:" << value["detimg"];
}

void fatal(string func)
{
    exit(1);
}

/// <summary>
/// 测试用，发送nng ipc消息
/// </summary>
/// <param name="url"></param>
/// <param name="msg"></param>
/// <returns></returns>
int sendnode(string url, string msg)
{
    int sz_msg = msg.length()+1; // '\0' too
    int sock;
    int rv;
    int bytes;

    if ((sock = nn_socket(AF_SP, NN_PUSH)) < 0) {
        fatal("nn_socket");
    }
    if ((rv = nn_connect(sock, url.c_str())) < 0) {
        fatal("nn_connect");
    }
    std::cout << "发送端：" << msg << std::endl;
    if ((bytes = nn_send(sock, msg.c_str(), sz_msg, 0)) < 0) {
        fatal("nn_send");
    }
    Sleep(1); // wait for messages to flush before shutting down
    return (nn_shutdown(sock, 0));
}

/// <summary>
/// 测试用，接受ipc nng消息
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
int recvnode(string url)
{
    int sock;
    int rv;

    if ((sock = nn_socket(AF_SP, NN_PULL)) < 0) {
        fatal("nn_socket");
    }
    if ((rv = nn_bind(sock, url.c_str())) < 0) {
        fatal("nn_bind");
    }
    char* buf = NULL;
    int bytes;
    while ((bytes = nn_recv(sock, &buf, NN_MSG, 0)) >= 0) {
        //int buflen = strlen(buf);
        //string recstr = buf;
       // std::cout <<"接收端str："<< recstr <<std::endl;
        std::cout <<"接收端buf："<< buf <<std::endl;
        nn_freemsg(buf);
    }
    fatal("nn_recv");
    return 0;
}
void IPCFun1(string namestr, string jsonstr)
{

    std::cout <<"函数1-"<< namestr << ":" << jsonstr << std::endl;
}
void IPCFun2(string namestr, string jsonstr)
{
    std::cout << "函数2-" << namestr << ":" << jsonstr << std::endl;
}
//接收端，1-开启接收端吗，2-结束接收端,0-程序结束
void InitIPCEngine()
{
    mNNG::NNGIPCEngine* mIPCEngin = mNNG::NNGIPCEngine::GetInstance();
    mIPCEngin->LoadUrlsFromConfig("W:\\GDUT\\NNGFragment\\NNGTest1\\testurls0.config");//读取config文件
    mIPCEngin->SetIPCFun("test1", IPCFun1);//设置回调函数
    mIPCEngin->SetIPCFun("test2", IPCFun2);

    string order_;
    while (std::cin >> order_)
    {
        if (order_ == "1")
        {
            mNNG::NNGIPCEngine::GetInstance()->Start();
        }
        else if (order_ == "2")
        {
            mNNG::NNGIPCEngine::GetInstance()->Stop();
        }
        else if (order_ == "3")
        {
            mNNG::NNGIPCEngine::GetInstance()->Stop();
        }
        else if (order_ == "0")
        {
            return ;
        }
    }
    
}

/// <summary>
/// 发送端，按1发送test1管道，按2发送test2管道
/// </summary>
/// <returns></returns>
int SendClient()
{
    string id_;
    string url_;
    while (std::cin >> id_ >> url_)
    {
        if (id_ == "1")mNNG::PushTaskByUrl("ipc:///tmp/pipeline1.ipc", url_);
        else if (id_ == "2")mNNG::PushTaskByUrl("ipc:///tmp/pipeline2.ipc", url_);
        else exit(0);
    }
}
int main()
{
    //SendClient();
    InitIPCEngine();
    
}
